﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace XNAStuding
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    
    class dragStand : DrawableGameComponent 
    {
        Vector2 mouseposition = new Vector2();
        Rectangle posD;
        Rectangle posTex;
        draw org;
        draw ext;
        draw ball;
        bool mouseOver;//check if the mouse pressed over the object or not
        bool movable;//check if the object can move or not 
        /// <summary>
        /// this class is used to dragwdrop the stand that is used in experiment 1
        /// </summary>
        /// <param name="game"></param>
        /// <param name="d">object from class draw represent 1st part of the texture that we will dragwdrop</param>
        /// <param name="posD">the position of the distintation rectangle in which the texture will be drew</param>
        /// <param name="posTex">the position of the source rectangle from which the texture will be drew</param>
        /// <param name="ext">object from class draw represent the 2nd part of the texture that we will dragwdrop</param>
        /// <param name="ball">object from class draw that represnt the ball</param>
        public dragStand(Game game,draw d,Rectangle posD,Rectangle posTex,draw ext,draw ball): base(game)
        {
            this.ball = ball;
            this.ext = ext;
            this.org = d;
            this.posD = posD;
            movable = true;
            this.posTex = posTex;
        }
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            ext.Visible = false;
            Globals.game.Components.Add(ext);
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            
                MouseState mouse_state = Mouse.GetState();
                mouseposition.X = mouse_state.X;
                mouseposition.Y = mouse_state.Y;
                Rectangle mouseRec = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1);
                //rectangele that contains the mouse 
                //if the two rectangels intersect each other 
                if (org.posD.Intersects(mouseRec) && mouse_state.LeftButton == ButtonState.Pressed && movable)
                {
                    mouseOver = true;
                }
                //if we pressed over the object 
                if (mouseOver)
                {
                    mouseOver = false;
                    org.posD.X = (int)mouseposition.X - (int)(129 / 2);
                    org.posD.Y = (int)mouseposition.Y - (int)(103 / 2);


                }
                //if we reached the right postion 
                if (org.posD.X >= posD.X && org.posD.X <= posD.X + 500 && org.posD.Y >= posD.Y && org.posD.Y <= posD.Y + 500 && mouse_state.LeftButton == ButtonState.Released)
                {
                    org.posTex = posTex;
                    org.posD.Width = posTex.Width;
                    org.posD.Height = posTex.Height;
                    org.posD.X = posD.X;
                    org.posD.Y = posD.Y;
                    if (ball.posD == new Rectangle(ext.posD.X + ext.posD.Width - 50, ext.posD.Y + ext.posD.Height, 33, 38) && Globals.t.go == false)
                    {
                        ball.posD = new Rectangle(ext.posD.X + ext.posD.Width - 50, ext.posD.Y + ext.posD.Height, 33, 38);
                        if (Globals.step == 1)
                            Globals.step = 2;
                    }
                    if (Globals.step == 0)
                        Globals.step = 1;
                    movable = false;
                    ext.Enabled = true;
                    ext.Visible = true;
                }
                base.Update(gameTime);
            
            
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {     org.Draw(gameTime);
                if (movable == false)
                    ext.Draw(gameTime);
                base.Draw(gameTime);
            
            

        }
    }
}